﻿Imports Sunts.HyperV.Common

Public Class frmBackupDevice
    Private Sub BackupDeviceSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillExistingDevices()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        fbdBrowse.ShowDialog()
        Me.txtDevicePath.Text = fbdBrowse.SelectedPath
        If Not txtDevicePath.Text.EndsWith("\") Then txtDevicePath.Text = txtDevicePath.Text & "\"
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Try
            For Each lvi As ListViewItem In Me.lstBackupDevices.Items
                If lvi.SubItems(2).Text = Me.txtDeviceName.Text Then
                    MsgBox("A backup device with the name " & Me.txtDeviceName.Text & " already exists, enter a different device name", CType(vbOKOnly + vbCritical, MsgBoxStyle), "Add Backup Device")
                    Exit Sub
                End If
            Next

            If Not IO.File.Exists(Me.txtDevicePath.Text & "\HyperVBackupTest.txt") Then
                IO.File.Create(Me.txtDevicePath.Text & "\HyperVBackupTest.txt").Close()
            End If

            Dim bd As New BackupDevice

            bd.DeviceName = Me.txtDeviceName.Text
            bd.DevicePath = Me.txtDevicePath.Text
            bd.Insert(My.Settings.ConnectionString)
            bd = Nothing

            FillExistingDevices()


            If IO.File.Exists(Me.txtDevicePath.Text & "\HyperVBackupTest.txt") Then
                IO.File.Delete(Me.txtDevicePath.Text & "\HyperVBackupTest.txt")
            End If

        Catch uae As System.UnauthorizedAccessException
            MsgBox("Access to path " & Me.txtDevicePath.Text & " is denied and the device was not added." & ControlChars.CrLf & _
                   "Check the share permissions and verify you have read/write access", CType(vbOKOnly + vbCritical, MsgBoxStyle), "Connection error")
        Catch dnfe As System.IO.DirectoryNotFoundException
            MsgBox("Could not add the device as the path is not accessible", CType(vbOKOnly + vbCritical, MsgBoxStyle), "Add Backup Device")
        Catch ex As Exception
            MsgBox(ex.Message, CType(vbOKOnly + vbCritical, MsgBoxStyle), "Add Backup Device")
        End Try
    End Sub

    Private Sub txtDeviceName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeviceName.TextChanged
        If Me.txtDevicePath.TextLength > 0 And Me.txtDeviceName.TextLength > 0 Then
            btnAdd.Enabled = True
        Else
            btnAdd.Enabled = False
        End If
    End Sub

    Private Sub txtDevicePath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDevicePath.TextChanged
        If Me.txtDevicePath.TextLength > 0 And Me.txtDeviceName.TextLength > 0 Then
            btnAdd.Enabled = True
        Else
            btnAdd.Enabled = False
        End If
    End Sub

    Private Sub FillExistingDevices()
        Try
            lstBackupDevices.Items.Clear()

            For Each bd As BackupDevice In BackupDevice.SelectAll(My.Settings.ConnectionString)
                Dim DeviceID As Int32 = bd.DeviceID
                Dim DeviceName As String = bd.DeviceName
                Dim DevicePath As String = bd.DevicePath
                Dim DeviceFreeSpace As Long = GetFreeSpace(DevicePath & "\")
                Dim ItemText As String

                If DeviceFreeSpace > 1024 Then
                    ItemText = DeviceName & ControlChars.CrLf & System.Math.Round(DeviceFreeSpace / 1024) & "GB Free"
                Else
                    ItemText = DeviceName & ControlChars.CrLf & DeviceFreeSpace & "MB Free"
                End If

                Dim itm As New ListViewItem(ItemText)
                itm.SubItems.AddRange({CStr(DeviceID), DeviceName, DevicePath, CStr(DeviceFreeSpace)})
                itm.ImageIndex = 3
                itm.ToolTipText = DevicePath

                lstBackupDevices.Items.Add(itm)
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub




End Class